import random
import d_luckdarw
import d_server
import time
import d_task
#prizeMultiple 抽奖倍数默认为1
class luckDarw:
    def __init__(self):
        if self.physicalValue < d_server.AutomaticAcquisition :
            if self.physicaTimer == 0 :
                self.physicaTimer = int(time.time())

    def turntable(self):
        #客户端获取抽奖结果
        if self.physicalValue < 1 :
            return 0
        size_chance = 0

        for key , v in d_luckdarw.Prize_pool.items():
            size_chance = size_chance + v["probability"]

        rand = random.randint(0, size_chance - 1)
        size_chance = 0
        darwId = 0
        for key, v in d_luckdarw.Prize_pool.items():
            size_chance = size_chance + v["probability"]
            if rand < size_chance :
                darwId = key
                print("sdfiowoiefw = %d"%(darwId))
                break

        print(self.prizeMultiple)
        self.consumePhysicalValue(1)
        if d_luckdarw.Prize_pool[darwId]["id"] == d_server.LuckDarwScore5 :
            self.addScore(d_luckdarw.Prize_pool[darwId]["num"],d_server.ScoreType.LuckDarw)
        elif d_luckdarw.Prize_pool[darwId]["id"] == d_server.LuckDarwScore10 :
            self.addScore(d_luckdarw.Prize_pool[darwId]["num"], d_server.ScoreType.LuckDarw)
        if d_luckdarw.Prize_pool[darwId]["rate"] > 7 or d_luckdarw.Prize_pool[darwId]["rate"] < 4 :
            self.addItem(d_luckdarw.Prize_pool[darwId]["id"],1*self.prizeMultiple)
        RemainingTime = 0;
        if self.physicaTimer != 0:
            RemainingTime = int(time.time()) - self.physicaTimer;
        #darwId 重1开始算
        print("s##########################################################")
        print(self.physicaTimer)
        print(self.physicalValue)
        print(RemainingTime)
        print(d_server.SetPhysicaTimer - RemainingTime)
        self.task_daily_update(d_task.task["mode_dial"]["value"] ,1,d_luckdarw.Prize_pool[darwId]["id"])
        self.client.turntableRsq(darwId , d_luckdarw.Prize_pool[darwId]["rate"],self.physicalValue,d_server.SetPhysicaTimer - RemainingTime,self.prizeMultiple)
        # 双倍奖励
        if darwId == d_server.Double:
            self.prizeMultiple *= 2
        else:
            self.prizeMultiple = 1


    def getPhysicalValue(self):     #获取体力值数
        #客户端获取体力
        RemainingTime = 0;
        if self.physicaTimer != 0 :
            RemainingTime = int(time.time()) - self.physicaTimer;
        self.client.physicalValueRsq(self.physicalValue, d_server.SetPhysicaTimer -RemainingTime)



    def addPhysicalValue(self,number): #增加的体力值数
        print(self.physicalValue)
        print(number)
        self.physicalValue  = self.physicalValue + number
        print("s##########################################################")
        print(self.physicalValue)
        self.client.setPhysicalValueRsq(self.physicalValue)
        if self.physicalValue >= d_server.AutomaticAcquisition and self.physicaTimer != 0 :
            self.physicaTimer = 0


    def consumePhysicalValue(self ,number):  #消耗的体力值数
        self.physicalValue = self.physicalValue - number
        if self.physicalValue < d_server.AutomaticAcquisition and self.physicaTimer == 0:
            self.physicaTimer = int(time.time())

    def addTimePhysical(self):
        #定时增加体力值
        if  self.physicalValue >= d_server.AutomaticAcquisition :
            self.physicaTimer = 0
            return
        else :
            self.addPhysicalValue(1)
            #self.physicaTimer = int(time.time())
            print("s##########################################################11111")
            print(self.physicaTimer)

    def timingTimeFun(self):
        curTime = int(time.time())
        if self.physicaTimer > 0 and curTime - self.physicaTimer > d_server.SetPhysicaTimer :
            add_num = int((curTime - self.physicaTimer )/ d_server.SetPhysicaTimer)
            for i in range(add_num) :
                self.addTimePhysical()
            if self.physicalValue < d_server.AutomaticAcquisition :
                self.physicaTimer = int(time.time()) - ((curTime - self.physicaTimer ) - d_server.SetPhysicaTimer * add_num)














